package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        try (Connection connection = DBUtil.getConnection();){
            Statement sta = connection.createStatement();
            String sql = "SELECT user.username,user.password,nickname\n" +
                    "FROM user\n" +
                    "WHERE username='"+username+"' AND password='"+ password+"'";
            ResultSet rs = sta.executeQuery(sql);
//
            if(rs.next()){
                String nickname = rs.getString("nickname");
                System.out.println("登录成功,欢迎【"+nickname+"】回来"
                );
            }else {
                System.out.println("登录失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
